package com.shy.collect.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shy.collect.entity.PayWay;
import com.shy.collect.service.PayWayService;
import com.shy.configs.common.enums.RCode;
import com.shy.configs.converts.PayWayConvert;
import com.shy.configs.mvc.models.AjaxResult;
import com.shy.configs.mvc.models.PageObject;
import com.shy.configs.mvc.models.query.PayWayQuery;
import com.shy.configs.mvc.models.vo.DicItemVo;
import com.shy.configs.mvc.models.vo.PayWayVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 支付方式操作
 */
@RestController
@RequestMapping("/payWay")
public class PayWayController extends BaseController {
    @Resource
    private PayWayService payWayService;
    @Resource
    private PayWayConvert payWayConvert;

    /**
     * 分页查询支付方式
     */
    @GetMapping("/pageList")
    public AjaxResult pageList(Integer pageNo, Integer pageSize, String productCode) {
        pageNo = defPageNo(pageNo);
        pageSize = defPageSze(pageSize);
        // 分页查询结果
        IPage<PayWay> payWayIPage = payWayService.pageList(productCode, pageNo, pageSize);
        // 分页对象
        PageObject pageObject = new PageObject(payWayIPage);
        List<PayWayVo> payWayVoList = payWayConvert.convertListPayWayVo(payWayIPage.getRecords());
        return AjaxResult.success(wrapData(payWayVoList, pageObject));
    }

    /**
     * 查询支付方式字典表
     *
     * @return
     */
    @GetMapping("/wayDicList")
    public AjaxResult dicList() {
        List<DicItemVo> dicItemVos = payWayService.getPayWayDicList();
        return AjaxResult.success(dicItemVos);
    }

    /**
     * 查询支付类型字典表
     */
    @GetMapping("/typeDicList")
    public AjaxResult typeDicList(String wayCode) {
        if (wayCode != null) {
            List<DicItemVo> dicItemVos = payWayService.getPayTypeDicList(wayCode.toUpperCase());
            return AjaxResult.success(dicItemVos);
        }
        return AjaxResult.fail(RCode.REQUEST_PARAM_ERROR);
    }

    /**
     * 查询参数是否有效,并添加支付方式
     */
    @PostMapping("/add")
    public AjaxResult createPayWay(@RequestBody PayWayQuery payWayQuery) {
        payWayService.createPayWay(payWayQuery);
        return AjaxResult.success();
    }
    /**
     * 删除支付方式
     */
    @PostMapping("/delete")
    public AjaxResult deletePayWay(@RequestBody PayWayQuery payWayQuery) {
        payWayService.removeById(payWayQuery);
        return AjaxResult.success();
    }
}
